package ods_job

import (
	"server/global"

	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

type OdsJob struct {
	OdsTbId uint `json:"odsTbId" form:"odsTbId" gorm:"column:odsTbId;comment:ods表id;size:100;"`
	OdsTbName string `json:"odsTbName" form:"odsTbName" gorm:"not null;column:odsTbName;comment:ods表名称;size:100;"`
	JobName string `json:"jobName" form:"jobName" gorm:"column:jobName;comment:任务名称;size:255;"`

	JobType string `json:"jobType" form:"jobType" gorm:"column:jobType;comment:任务类型;size:32;"`
	SyncType string `json:"syncType" form:"syncType" gorm:"column:syncType;comment:同步方式;size:255;"`
	SyncColName string `json:"syncColName" form:"syncColName" gorm:"column:syncColName;comment:同步列名称;size:100;"`
	Step        int    `json:"step" form:"step" gorm:"column:step;comment:同步步长;"`
	PageLimit   int    `json:"pageLimit" form:"pageLimit" gorm:"column:pageLimit;comment:分页查询数量;default:10000"`

	ScopeString string `json:"scopeString" form:"scopeString" gorm:"column:scopeString;comment:同步范围语句;size:50;"`

	TimeingSet string `json:"timeingSet" form:"timeingSet" gorm:"column:timeingSet;comment:定时同步设置;size:50;"`
	PeriodSet  string `json:"periodSet" form:"periodSet" gorm:"column:periodSet;comment:同期同步设置;size:50;"`

	JobGroup string `json:"jobGroup" form:"jobGroup" gorm:"column:jobGroup;comment:任务分组;size:255;"`

	CronExpression string `json:"cronExpression" form:"cronExpression" gorm:"column:cronExpression;comment:cron表达式;size:255;"`

	Concurrent bool `json:"concurrent" form:"concurrent" gorm:"column:concurrent;comment:是否并发;size:32;"`

	Ready int `json:"ready" form:"ready" gorm:"column:ready;comment:配置检查;size:32;"`
	Start bool `json:"start" form:"start" gorm:"column:start;comment:是否启动;size:32;"`
	State int `json:"state" form:"state" gorm:"column:state;comment:运行状态;size:32;"`

	Remarks string `json:"remarks" form:"remarks" gorm:"column:remarks;comment:备注;size:191;"`
	global.BaseModel
	global.ControlBy

	InOdsTb int `json:"inOdsTb" gorm:"-"`
	InTimer int `json:"inTimer" gorm:"-"`
	IsRunning int `json:"isRunning" gorm:"-"`
}

type ActiveRecord interface {
	schema.Tabler
	SetCreateBy(createBy int)
	SetUpdateBy(updateBy int)
	Generate() ActiveRecord
	GetId() interface{}
}


func (m *OdsJob) TableName() string {
	return "ods_job"
}

func (m *OdsJob) Create() error {
	err := global.SysDB.Create(m).Error
	return err
}

func (m *OdsJob) Save() error {
	m.SetReady()
	if m.ID == 0 {
		if err := m.Create(); err != nil {
			return err
		}
	}
	err := global.SysDB.Where("id=?", m.ID).Save(&m).Error
	return err
}

func (m *OdsJob) SetReady() {
	if m.JobType == "实时同步" {
		m.Ready = 1
		return
	}

	if m.JobType == "定时同步" {
		if m.SyncType != "" && m.CronExpression != "" {
			m.Ready = 1
			return
		}
	}

	if m.JobType == "同步一次" && m.SyncType != "" {
		m.Ready = 1
		return
	}
	m.Ready = 0
}

func (m *OdsJob) CheckInTimer() {
	corn, ok := global.XidwTimer.FindCron(m.OdsTbName)
	if ok && corn != nil {
		m.IsRunning = 1
	} else {
		m.IsRunning = 0
	}
}

func (m *OdsJob) Generate() ActiveRecord {
	o := *m
	return &o
}

func (m *OdsJob) GetId() interface{} {
	return m.ID
}

func (m *OdsJob) SetCreateBy(createBy int) {
	m.CreateBy = createBy
}

func (m *OdsJob) SetUpdateBy(updateBy int) {
	m.UpdateBy = updateBy
}

func (m *OdsJob) GetList(tx *gorm.DB, list interface{}) (err error) {
	return tx.Table(m.TableName()).Where("state = ?", 2).Find(list).Error
}

func (m *OdsJob) Update(gormDB *gorm.DB, id interface{}) (err error) {
	return gormDB.Table(m.TableName()).Where(id).Updates(&m).Error
}

func (m *OdsJob) RemoveAllEntryID(gormDB *gorm.DB) (update OdsJob, err error) {
	if err = gormDB.Table(m.TableName()).Where("entry_id > ?", 0).Update("entry_id", 0).Error; err != nil {
		return
	}
	return
}
